/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @author jase
 */
public class Execute {

	// similar to process() but runs on a separate thread
	public static void runSeparate(final String commandStr) {

		Thread thread = new Thread() {

			@Override
			public void run() {
				process(commandStr);
			}
		};
		thread.start();
	}

	// the process is run - relative to the java application directory!!!!! :)))
	public static String process(String commandStr) {
		StringBuffer buf = new StringBuffer();
		try {
			Runtime run = Runtime.getRuntime();
			Process p = run.exec(commandStr);

			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
			while (true) {
				String line = reader.readLine();
				if (line == null) {
					break;
				}
				buf.append(line + "\n\n");
			}
			reader.close();
			reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			while (true) {
				String line = reader.readLine();
				if (line == null) {
					break;
				}
				buf.append(line + "\n");
			}
			p.waitFor();
			reader.close();

		} catch (Throwable t) {
			//System.out.println("error: " + e);
		}

		return buf.toString();
	}
}
